/*
 * @Author: TC(tengchao@taqu.cn)
 * @Date: 2025-11-03 14:48:40
 * @Last Modified by: TC(tengchao@taqu.cn)
 * @Last Modified time: 2025-11-03 17:48:55
 */

import { _decorator, Component, director, EventTouch, instantiate, Label, Node, tween, UITransform, v3 } from "cc";
import { BaseUI } from "db://assets/FrameWork/Module/Component/BaseUI";
import { gameShaderCase } from "../../GameShaderCase";
import { app } from "db://assets/FrameWork/App";
import { EShaderCaseBundleConst } from "../../Const/ShaderCaseBundleConst";
const { ccclass, property } = _decorator;

/**
 * 主界面
 */
@ccclass("ShaderCaseMainUI")
export class ShaderCaseMainUI extends BaseUI {
    @property({ type: Node, displayName: "克隆按钮" })
    nodeClone: Node = null;
    @property({ type: Node, displayName: "按钮列表" })
    layoutBtn: Node = null;
    @property({ type: Node, displayName: "箭头按钮" })
    btnArrow: Node = null;
    @property({ type: Node, displayName: "滚动列表" })
    sv: Node = null;

    private _aning = false; //是否在播放动画
    private _currCase = "";
    private _nodeCase: Node = null; //案例节点

    protected init(): void {
        super.init();
        this._initBtnList();
    }

    protected addUIEvent(): void {
        super.addUIEvent();
        this.addClickEvent(this.btnArrow, this._onArrow);
    }

    /**
     * 初始化按钮列表
     */
    private _initBtnList(): void {
        const cfgs = gameShaderCase.cfgMgr.getCaseCfgs();
        cfgs.forEach((cfg) => {
            const node = instantiate(this.nodeClone);
            node.name = cfg.nameCN;
            node.getChildByName("lbl").getComponent(Label).string = cfg.nameCN;
            this.addClickEvent(node, this._onCase);
            this.layoutBtn.addChild(node);
        });
    }

    /**
     * 点击按钮
     */
    private _onArrow(): void {
        if (this._aning) return;
        this._aning = true;
        const scaleX = this.btnArrow.children[0].scale.x;
        this.btnArrow.children[0].setScale(v3(-scaleX, 1, 1));

        if (scaleX < 0) {
            tween(this.sv)
                .to(0.2, { position: v3(-170, 0, 0) })
                .call(() => {
                    this._aning = false;
                })
                .start();
        } else {
            tween(this.sv)
                .to(0.2, { position: v3(-1200, 0, 0) })
                .call(() => {
                    this._aning = false;
                })
                .start();
        }
    }

    /**
     * 点击案例
     * @param e
     */
    private _onCase(e: EventTouch): void {
        const name = e.target.name;
        if (this._currCase == name) return;
        this._currCase = name;
        app.resMgr.loadPrefab(EShaderCaseBundleConst.ShaderCase, `Cases/${name}/prefab`, (p) => {
            if (this._nodeCase) {
                this._nodeCase.destroy();
            }
            const node = instantiate(p);
            this._nodeCase = node;
            director.getScene().addChild(node);
        });
    }
}
